package com.haojiu.controller;

import com.github.pagehelper.PageInfo;
import com.haojiu.pojo.Admin;
import com.haojiu.pojo.User;
import com.haojiu.service.AdminService;
import com.haojiu.service.UserService;
import com.haojiu.util.ExportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:我叫浩九
 * @date：2021/8/30 10:30
 * create in RuGao JiangSu
 */
@Controller
public class AdminController {
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    @RequestMapping("/adminIndex")
    public ModelAndView queryAllAdmin(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size,HttpServletRequest request) {
        HttpSession session = request.getSession();
        String userType = (String) session.getAttribute("userType");
        ModelAndView mv = new ModelAndView();
        List<User> list = userService.getAllManager("manager",page, size);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        mv.addObject("pageInfo", pageInfo);
        if(userType.equals("manager")){
            mv.setViewName("admin/allAdmin");
        }else {
            mv.setViewName("admin/allAdmin2");
        }

        return mv;
    }

    @RequestMapping("/login/delAdmin/{adminName}")
    public String delAdmin(@PathVariable("adminName") String adminName) {
        userService.deleteUserByName(adminName);
        return "redirect:/adminIndex";
    }

    @RequestMapping("/login/toUpdateAdmin")
    public String toUpdateAdmin(Model model, String adminName) {
        User user = userService.queryUserByName(adminName);
        System.out.println(user);
        model.addAttribute("admin", user);
        return "admin/updateAdmin";
    }

    @RequestMapping("/login/updateAdmin")
    public String updateAdmin(Model model, User user) {
        System.out.println(user);
        userService.updateUser(user);
        User admin1 = userService.queryUserByName(user.getUsername());
        model.addAttribute("admin", admin1);
        return "redirect:/adminIndex";
    }

    @RequestMapping("/login/toAddAdmin")
    public String toAddAdmin() {
        return "admin/addAdmin";
    }

    @RequestMapping("/login/addAdmin2")
    public String addUser(User user) {
        System.out.println(user);
        userService.addUser(user);
        return "redirect:/adminIndex";
    }

    @RequestMapping("/login/searchAdmin")
    public String getAdmin(@RequestParam("keyAdminName") String adminName, Model model) {
        User user = userService.queryUserByName(adminName);
        if (user != null) {
            List<User> adminList = new ArrayList();
            adminList.add(user);
            model.addAttribute("one", adminList);
            return "admin/oneAdmin";
        } else {
            return "redirect:/adminIndex";
        }
    }
    @RequestMapping("/login/exportAdmin")
    public void export(HttpServletRequest request, HttpServletResponse response){
        List<User> adminList = userService.getAllManager("manager");
        ExportExcel<User> ee= new ExportExcel<User>();
        String[] headers = {"管理员名称","登录密码","管理员类型"};
        String fileName = "阅读之家管理员excel表";
        ee.exportExcel(headers,adminList,fileName,response);
    }
}
